package algorithm.middle;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: algorithm.middle
 * @Author: 江岸
 * @CreateTime: 2022-11-17 22:25
 * @Description: 给定字符串 s 和字符串数组 words, 返回  words[i] 中是s的子序列的单词个数 。
 * <p>
 * 字符串的 子序列 是从原始字符串中生成的新字符串，可以从中删去一些字符(可以是none)，而不改变其余字符的相对顺序。
 * https://leetcode.cn/problems/number-of-matching-subsequences/solutions/1975581/-by-muse-77-1vhl/
 */
public class NumMatchingSubseq {

    public int numMatchingSubseq(String s, String[] words) {
        char[] arr = s.toCharArray();
        int count = 0;
        for (String word : words) {
            if (word.length() > arr.length) continue;
            if (compare(arr,word)){
                count++;
            }
        }
        return count;
    }

    public boolean compare(char[] arr, String word) {
        char[] words = word.toCharArray();
        int i = 0;
        int j = 0;
        while (i<words.length && j<arr.length){
            if (words[i]==arr[j]){
                i++;
                j++;
            }else {
                j++;
            }
        }
        return i == words.length;
    }
}
